package cn.samir.sudoku.ui;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;

import cn.samir.sudoku.net.OnNetListener;
import cn.samir.sudoku.util.LogUtils;
import cn.samir.sudoku.util.Utils;
import cn.samir.sudoku.view.LoadingDialog;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.PlatformDb;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.tencent.qq.QQ;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Notification;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.Window;

public class BaseActivity extends Activity implements PlatformActionListener {
	protected String TAG = "BaseActivity";
	public static ArrayList<Activity> activities = new ArrayList<Activity>();
	public Context mContext;
	public AlertDialog alertDialog;
	public LoadingDialog loadingDialog;

	Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 1:
				HashMap<String, String> params2 = (HashMap<String, String>) msg.obj;
				loginToServer(params2);
				break;

			default:
				break;
			}
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		activities.add(this);
		mContext = this;
		loadingDialog = new LoadingDialog(this);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		activities.remove(this);
	}

	@SuppressWarnings("unchecked")
	<T> T getView(int id) {
		return (T) this.findViewById(id);
	}

	public void loginQQ() {
		loadingDialog.show("正在初始化");
		ShareSDK.initSDK(this);
		Platform platform = ShareSDK.getPlatform(QQ.NAME);
		platform.SSOSetting(false);
		platform.setPlatformActionListener(this);
		platform.showUser(null);
	}

	@Override
	public void onCancel(Platform arg0, int arg1) {
		LogUtils.d(TAG, "onCancel");
	}

	@Override
	public void onComplete(Platform platform, int arg1,
			HashMap<String, Object> param) {
		LogUtils.d(TAG, "onComplete");
		loginsuccess(platform, param);
	}

	public void loginsuccess(Platform platform, HashMap<String, Object> param) {
		LogUtils.d(TAG, "loginsuccess");
		final PlatformDb db = platform.getDb();
		final String userIcon = db.getUserIcon();
		final String id = db.getUserId();
		String gender = db.getUserGender();
		String nickname = db.getUserName();
		String figureurl_qq_2 = userIcon;
		if (param.containsKey("figureurl_qq_2")) {
			figureurl_qq_2 = param.get("figureurl_qq_2") + "";
			Utils.setUserBigHeaderUrl(mContext, figureurl_qq_2);
		}

		// Utils.putSharedPre(this, "figureurl_qq_2", figureurl_qq_2);
		Utils.setUserId(mContext, id);
		Utils.setUserIcon(mContext, userIcon);
		Utils.setUserName(mContext, nickname);
		Utils.setUserGender(mContext, gender);
		// Utils.putSharedPre(this, "userId", id);
		// Utils.putSharedPre(this, "userIcon", userIcon);
		// Utils.putSharedPre(this, "gender", gender);
		// Utils.putSharedPre(this, "nickname", nickname);
		// Utils.netFile(userIcon, "icon40_" + id + ".png");
		// Utils.netFile(figureurl_qq_2, "icon100_" + id + ".png");

		HashMap<String, String> params2 = new HashMap<String, String>();
		params2.put("username", id);
		params2.put("usericon", figureurl_qq_2);
		params2.put("gender", gender);
		params2.put("nickname", nickname);
		params2.put("requestcode", 10002 + "");
		Message message = Message.obtain();
		message.obj = params2;
		message.what = 1;
		handler.sendMessage(message);
		LogUtils.d(TAG, "loginsuccess end");
	}

	public void loginToServer(HashMap<String, String> params2) {
		Utils.info(this, params2, new OnNetListener() {

			@Override
			public void onSuccess(InputStream in) {

			}

			@Override
			public void onSuccess(String result) {
				LogUtils.d(TAG, result);
				if (!TextUtils.isEmpty(result)) {
					JSONObject jsonObject;
					try {
						jsonObject = new JSONObject(result);
						int code = jsonObject.getInt("code");
						int id = (code - 33) / 76;
						Utils.putSharedPre(mContext, "code", id + "");
					} catch (JSONException e) {
						e.printStackTrace();
					}

				}
				loadingDialog.dismiss();
			}

			@Override
			public void onError(String result) {
				LogUtils.showShortToast(mContext, "失败。");
				Utils.loginOut(mContext);
				loadingDialog.dismiss();
			}
		});
	}

	@Override
	public void onError(Platform arg0, int arg1, Throwable e) {
		LogUtils.i(TAG, "onError" + e.getLocalizedMessage());
		loadingDialog.dismiss();
	}

	public void showLoginDialog(String msg) {
		alertDialog = new AlertDialog.Builder(mContext)
				.setTitle("您还未登陆")
				.setMessage(msg)
				.setPositiveButton("去登陆",
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								loginQQ();
							}
						}).setPositiveButton("取消", null).show();
	}
}
